function [use_parallel, pool_info] = parallel_manager()
% 并行计算管理器
% 输出参数：
%   use_parallel - 是否使用并行计算 (布尔值)
%   pool_info - 并行池信息 (结构体)

% 初始化输出
use_parallel = false;
pool_info = struct();
pool_info.num_cores = feature('numcores');
pool_info.pool_size = 0;
pool_info.pool_status = 'not_available';

% 检查并行计算工具箱是否可用
if ~license('test', 'Distrib_Computing_Toolbox')
    fprintf('并行计算工具箱不可用，使用串行计算\n');
    pool_info.pool_status = 'toolbox_not_available';
    return;
end

try
    % 检查当前是否有活动的并行池
    current_pool = gcp('nocreate');
    
    if isempty(current_pool)
        % 尝试启动并行池
        try
            % 使用本地worker，数量为核心数减1（留一个核心给系统）
            num_workers = max(1, pool_info.num_cores - 1);
            parpool('local', num_workers);
            
            % 获取并行池信息
            current_pool = gcp();
            use_parallel = true;
            pool_info.pool_size = current_pool.NumWorkers;
            pool_info.pool_status = 'started';
            
            fprintf('并行池已启动，使用 %d 个worker\n', pool_info.pool_size);
            
        catch pool_error
            fprintf('并行池启动失败: %s\n', pool_error.message);
            pool_info.pool_status = 'failed_to_start';
            use_parallel = false;
        end
    else
        % 并行池已存在
        use_parallel = true;
        pool_info.pool_size = current_pool.NumWorkers;
        pool_info.pool_status = 'already_running';
        fprintf('使用已存在的并行池，worker数量: %d\n', pool_info.pool_size);
    end
    
catch error
    fprintf('并行计算初始化失败: %s\n', error.message);
    pool_info.pool_status = 'initialization_failed';
    use_parallel = false;
end

end